package com.atguigu.lease.web.admin.controller.lease;


import com.atguigu.lease.common.result.Result;
import com.atguigu.lease.model.entity.LeaseAgreement;
import com.atguigu.lease.model.enums.LeaseStatus;
import com.atguigu.lease.web.admin.service.LeaseAgreementService;
import com.atguigu.lease.web.admin.vo.agreement.AgreementQueryVo;
import com.atguigu.lease.web.admin.vo.agreement.AgreementVo;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@Tag(name = "租约管理")
@RestController
@RequestMapping("/admin/agreement")
public class LeaseAgreementController {

    @Autowired
    private LeaseAgreementService leaseAgreementService;

    @Operation(summary = "保存或修改租约信息")
    @PostMapping("saveOrUpdate")
    public Result saveOrUpdate(@RequestBody LeaseAgreement leaseAgreement) {
        leaseAgreementService.saveOrUpdate(leaseAgreement);
        return Result.ok();
    }

    @Operation(summary = "根据条件分页查询租约列表")
    @GetMapping("page")
    public Result<IPage<AgreementVo>> page(@RequestParam long current, @RequestParam long size, AgreementQueryVo queryVo) {
        IPage<AgreementVo> page=new Page<>(current,size);
        leaseAgreementService.custompage(page,queryVo);
        return Result.ok(page);
    }

    /**
     * 上方根据条件分页查询租约列表的返回值和该方法根据Id查询租约信息的返回值是相同的。都是AgreementVo
     * 区别在于上方有分页，该方法没有。上方可能会有多条，该方法只会有一条。
     * 分页解决，当前在第一页，页面的size够大就相当于没有分页
     * 一条和多条，在查询完成后从page把第一个元素（下标为0）的元素提取出来，就是我们要的一条的结果。
     * 上方根据省份，城市，公寓，姓名，手机号等信息查，下方根据租约id查。
     * 要在接受参数的VO类AgreementQueryVo中添加一条属性id。我们手动创建AgreementQueryVo对象，setid进去就有id了。
     * 然后查询的动态sql里添加一个<if>标签，如果id不为空，就多判断一个条件。
     * 这些改动不会影响根据条件分页查询的任何功能。
     * @param id
     * @return
     */
    @Operation(summary = "根据id查询租约信息")
    @GetMapping(name = "getById")
    public Result<AgreementVo> getById(@RequestParam Long id) {
        IPage<AgreementVo> page=new Page<>(1,100);
        AgreementQueryVo agreementQueryVo=new AgreementQueryVo();
        agreementQueryVo.setId(id);
        leaseAgreementService.custompage(page,agreementQueryVo);
        AgreementVo vo=page.getRecords().get(0);
        return Result.ok(vo);
    }

    @Operation(summary = "根据id删除租约信息")
    @DeleteMapping("removeById")
    public Result removeById(@RequestParam Long id) {
        leaseAgreementService.removeById(id);
        return Result.ok();
    }

    @Operation(summary = "根据id更新租约状态")
    @PostMapping("updateStatusById")
    public Result updateStatusById(@RequestParam Long id, @RequestParam LeaseStatus status) {
        LambdaUpdateWrapper<LeaseAgreement> leaseAgreementLambdaUpdateWrapper=new LambdaUpdateWrapper<>();
        leaseAgreementLambdaUpdateWrapper.eq(LeaseAgreement::getId,id);
        leaseAgreementLambdaUpdateWrapper.set(LeaseAgreement::getStatus,status);
        leaseAgreementService.update(leaseAgreementLambdaUpdateWrapper);
        return Result.ok();
    }

}

